package com.xyq.system.service.user;

import com.github.pagehelper.PageHelper;
import com.xyq.common.core.enums.ResultCode;
import com.xyq.common.core.exception.ServiceException;
import com.xyq.system.domain.user.User;
import com.xyq.system.domain.user.dto.UserDTO;
import com.xyq.system.domain.user.dto.UserStatusDTO;
import com.xyq.system.manager.UserCacheManager;
import com.xyq.system.mapper.user.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper ;

    @Autowired
    private UserCacheManager userCacheManager ;

    @Override
    public List<User> list(UserDTO userDTO) {
        // 加入这条语句就足够可以实现 分页查询功能
        PageHelper.startPage(userDTO.getPageNum() , userDTO.getPageSize());
        return  userMapper.selectUserList(userDTO) ;
    }

    // 拉黑解禁 用户
    @Override
    public int updateUserStatus(UserStatusDTO userStatusDTO) {
         User user = userMapper.selectById(userStatusDTO.getUserId());
         if(user == null){
             throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
         }

          user.setStatus(userStatusDTO.getStatus());
          userCacheManager.refreshUser(user);
        return userMapper.updateById(user);
    }


}
